package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

//reqeust
func main() {
	//如何设置请求的查询参数,
	//定制请求头
	//requestByHead()
	requestByParams()
}

func printBody(r *http.Response) {
	context, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", context)
}

func requestByParams() {
	r, err := http.NewRequest(http.MethodGet, "http://httpbin.org/get", nil)
	if err != nil {
		panic(err)
	}
	params := make(url.Values)
	params.Add("name", "ploxue")
	params.Add("age", "18")
	fmt.Println(params.Encode())
	r.URL.RawQuery = params.Encode()

	//封装好了参数发起请求
	r1, err := http.DefaultClient.Do(r)
	if err != nil {
		panic(err)
	}
	defer func() {
	_:
		r1.Body.Close()
	}()
	fmt.Println(r1)
}

func requestByHead() {
	r, err := http.NewRequest(http.MethodGet, "http://httpbin.org/get", nil)
	if err != nil {
		panic(err)
	}
	r.Header.Add("user-agent", "chorme")
	r2, err2 := http.DefaultClient.Do(r)
	if err2 != nil {
		panic(err2)
	}
	defer func() {
	_:
		r2.Body.Close()
	}()
	fmt.Println(r2)
}
